package com.zhonggong.empProducer.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.zhonggong.empProducer.dao.ProducerDao;
import com.zhonggong.empProducer.dao.impl.ProducerDaoImpl;
import com.zhonggong.empProducer.service.ProducerManager;
import com.zhonggong.network.emp.model.Emp;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ProducerManagerImpl implements ProducerManager {
	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	public List<Emp> empsList;
	ProducerDao producerDao = new ProducerDaoImpl();
	
	public void loadAdd(String data_file) {
		this.empsList = new ArrayList<Emp>();
		InputStream input = ProducerManagerImpl.class.getResourceAsStream(data_file);
		XSSFWorkbook wb = null;
		try {
			wb = new XSSFWorkbook(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		XSSFSheet sheet = wb.getSheetAt(1);
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Emp s = new Emp();
			/**
			 * Set the system unique Id for object
			 */
			s.setId(Long.valueOf(1000 + i));
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = row.getCell(j).getStringCellValue().trim();
					if (j == 0)
						s.setName(value);
					if (j == 1)
						s.setGongzi(value);
					if (j == 2)
						s.setBumen(value);
				}
			}
			logger.info(" emp_" + i + " is :" + s);
			this.empsList.add(s);
			this.producerDao.add(s); // 添加到数据库
		}
		logger.info("size of emp list is :" + this.empsList.size());

	}

	public Emp findByNameEmp(String name) {
		return producerDao.findByName(name);
	}

	public List<Emp> queryAllEmp() {
		return producerDao.queryall();
	}

	public void upDateEmp(Emp emp) {
		producerDao.upDate(emp);

	}

	public void deleteEmp(Emp emp) {
		producerDao.delete(emp);
	}

	public String json(List<Emp> empsList) {
		
		Iterator<Emp> iterator = empsList.iterator();
		
		JSONObject j = new JSONObject();
		JSONArray array = new JSONArray();
		while (iterator.hasNext()) {
			Emp emp = new Emp();
			emp = iterator.next();
			JSONObject jemp = new JSONObject();
			jemp.accumulate("name", emp.getName());
			jemp.accumulate("gongzi", emp.getGongzi());
			jemp.accumulate("bumen", emp.getBumen());
			array.add(jemp);
		}
		j.accumulate("Emp", array);
	
		return j.toString();
	}
	
	public void send() {
		this.empsList = new ArrayList<Emp>();
		empsList = producerDao.queryall(); // 从数据库中取出computer
		Producer p = new Producer();
		try {
			ConnectionFactory factory = new ActiveMQConnectionFactory(p.url);
			Connection connection = factory.createConnection();
			connection.start();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(p.queueName);
			MessageProducer producer = session.createProducer(destination);
			Serialize s = new Serialize();
			byte[] bt = s.serialize(empsList);
			BytesMessage bmessage = session.createBytesMessage();
			bmessage.writeBytes(bt);
			producer.send(bmessage);
			connection.close();
		} catch (JMSException e) {
			logger.info("异常！");
			e.printStackTrace();
		}
	}
	
}